<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useCountDown } from '../utils/useCountDown.js'
import { ElMessage } from 'element-plus'
import Link from "../api/Link.js";
import apiurl from "../api/url.js";
const { formatTime, start } = useCountDown()
import { useRoute, useRouter } from 'vue-router'
const route = useRoute()
const router = useRouter()

onMounted(() => getPayInfo())

// 获取订单数据
let payInfo = ref({})

const getPayInfo = async () => {
    console.log(route.query.id);
    console.log(route.query.time);

    await Link(apiurl.get, "GET", {}, { orderId: route.query.id })
        .then((ok) => {
            console.log(ok)
            payInfo.value = ok
        })
    console.log("支付信息的数据", payInfo.value);
    // 初始化倒计时秒数
    start(route.query.time)
    if (payInfo.value.orderState == "已支付") {
        //已经支付提示框
        ElMessage({
            showClose: true,
            message: '订单已经支付，正在跳转主页...',
            type: 'success',
        })
        const message = document.querySelector('.el-message__content')?.parentNode
        //判断提示框的数量   
        if (document.getElementsByClassName('el-message__content').length > 1) {
            setTimeout(function () {
                message.parentNode.removeChild(message);
            }, 100)
        }
        setTimeout(function () {
            router.push({
                path: '/main',
            })
        }, 1000)
    }
}

//订单支付
let orderId = route.query.id
const to_payback = async () => {
    //打开支付宝的支付接口
    window.open(apiurl.pay + "?orderId=" + orderId)
}
</script>

<template>
    <div class="xtx-pay-page">
        <div class="container">
            <!-- 付款信息 -->
            <div class="pay-info">
                <span class="icon iconfont icon-queren2"></span>
                <div class="tip">
                    <p>订单提交成功！请尽快完成支付。</p>
                    <p>支付还剩 <span>{{ formatTime }}</span>, 超时后将取消订单</p>
                </div>
                <div class="amount">
                    <span>应付总额：</span>
                    <span>¥{{ payInfo.payPrice }}</span>
                </div>
            </div>
            <!-- 付款方式 -->
            <div class="pay-type">
                <p class="head">选择以下支付方式付款</p>
                <div class="item">
                    <p>支付平台</p>
                    <a class="btn wx" href="javascript:;"></a>
                    <a class="btn alipay" @click="to_payback"></a>
                </div>
                <div class="item">
                    <p>支付方式</p>
                    <a class="btn zs" href="javascript:;"></a>
                    <a class="btn gs" href="javascript:;"></a>
                    <a class="btn js" href="javascript:;"></a>
                    <a class="btn ny" href="javascript:;"></a>
                    <a class="btn jt" href="javascript:;"></a>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped lang="scss">
.xtx-pay-page {
    margin-top: 20px;
}

.pay-info {

    background: #fff;
    display: flex;
    align-items: center;
    height: 240px;
    padding: 0 80px;

    .icon {
        font-size: 80px;
        color: #1dc779;
    }

    .tip {
        padding-left: 10px;
        flex: 1;

        p {
            &:first-child {
                font-size: 20px;
                margin-bottom: 5px;
            }

            &:last-child {
                color: #999;
                font-size: 16px;
            }
        }
    }

    .amount {
        span {
            &:first-child {
                font-size: 16px;
                color: #999;
            }

            &:last-child {
                color: black;
                font-size: 20px;
            }
        }
    }
}

.pay-type {
    margin-top: 20px;
    background-color: #fff;
    padding-bottom: 70px;

    p {
        line-height: 70px;
        height: 70px;
        padding-left: 30px;
        font-size: 16px;

        &.head {
            border-bottom: 1px solid #f5f5f5;
        }
    }

    .btn {
        width: 150px;
        height: 50px;
        border: 1px solid #e4e4e4;
        text-align: center;
        line-height: 48px;
        margin-left: 30px;
        color: #666666;
        display: inline-block;

        &.active,
        &:hover {
            border-color: green;
        }

        &.alipay {
            background: url("../assets/image/zfb.png") no-repeat center / contain;
        }

        &.wx {
            background: url("../assets/image/wx.jpg") no-repeat center / contain;
        }

        &.zs {
            background: url("../assets/image/zs.jpg") no-repeat center / contain;
        }

        &.gs {
            background: url("../assets/image/gs.jpg") no-repeat center / contain;
        }

        &.js {
            background: url("../assets/image/js.png") no-repeat center / contain;
        }

        &.ny {
            background: url("../assets/image/ny.png") no-repeat center / contain;
        }

        &.jt {
            background: url("../assets/image/jt.png") no-repeat center / contain;
        }

    }
}
</style>